//
//  BGOAnalyticsManager.h
//  Bango
//

#import <CoreLocation/CoreLocation.h>

@class BGOProfile;
@class BGOEvent;
@class BGOEventParameter;
@protocol BGOAnalyticsManagerDelegate;

@interface BGOAnalyticsManager : NSObject <UIWebViewDelegate, CLLocationManagerDelegate> {
@private
	// config options
	NSString *_applicationChannel;
	NSString *_applicationType;
	NSString *_applicationVersion;
	int _continueSessionSeconds;
	BOOL _findLocation;
	BOOL _automaticDataTransmission;
	NSString *_postUrl;
	NSString *_userId;
	NSString *_ipAddress;
	
	// stores if the device can make calls
	BOOL _deviceIsMobile;
    
	// For obtaining useragent
	NSString *_userAgent;
	NSString *_xUserAgent;
	UIWebView *_webView;
	
	// pointer for profile being submitted
	BGOProfile *_transmittingProfile;
    
    // stores time of transmission start
    NSDate *_transmissionStartDate;
	
	// core locations manager
	CLLocationManager *_locationManager;
	
	// for transmission
	BOOL _transmitting;
	
	// for debug log
	BOOL _debugLog;
	
    id<BGOAnalyticsManagerDelegate> _delegate;
    
	// stores the current transmission mode.
	NSString *_transmissionMode;
	
	// The data being asynchronously received from bango
	NSMutableData *_responseData;
   
    int                  _loggingLevel;
    NSMutableDictionary *_loggingEvents;
    BOOL                 _loggingEnabled;
    BOOL                 _eventThrottling;
}

////////////////////////////////////////////////////////////////////////////
// accessor generators

// the fields dictionaries
@property (nonatomic, retain) NSMutableDictionary *profileFieldsDictionary;
@property (nonatomic, retain) NSMutableDictionary *eventFieldsDictionary;

// for config
@property (nonatomic, retain) NSString *applicationChannel;
@property (nonatomic, retain) NSString *applicationType;
@property (nonatomic, retain) NSString *applicationVersion;
@property (nonatomic, readwrite) int continueSessionSeconds;
@property (nonatomic, readwrite) BOOL findLocation;
@property (nonatomic, readwrite) BOOL automaticDataTransmission;
@property (nonatomic, retain) NSString *postUrl;
@property (nonatomic, retain) NSString *userId;
@property (nonatomic, retain) NSString *ipAddress;

@property (nonatomic, readwrite) BOOL deviceIsMobile;

// useragent
@property (nonatomic, retain) NSString *userAgent;
@property (nonatomic, retain) NSString *xUserAgent;

// transmitting profile
@property (nonatomic, retain) BGOProfile *transmittingProfile;
@property (nonatomic, retain) NSMutableData *responseData;

// for the date
@property (nonatomic, retain) NSDate *transmissionStartDate;

// core locations
@property (nonatomic, retain) CLLocationManager *locationManager;

// transmitting
@property (nonatomic, readwrite) BOOL transmitting;


// transmission mode
@property (nonatomic, retain) NSString *transmissionMode;

// debug
@property (nonatomic, getter = isDebugLog, setter = setDebugLog:) BOOL debugLog;

// delegate
@property (nonatomic, retain) id<BGOAnalyticsManagerDelegate> delegate;

// The maximum number of events to be posted to bango at any time
@property (nonatomic, assign) int postChunkSize;

// Do not ever set this value to yes as your app might get rejected
@property (nonatomic, assign) BOOL useLegacyUdidMode;

// Logging level

@property (nonatomic, readwrite)                      int                  loggingLevel;
@property (assign,nonatomic,getter=isLoggingEnabled)  BOOL                 logEnabled;
@property (assign,nonatomic,getter=isEventThrottling) BOOL                 eventThrottling;

////////////////////////////////////////////////////////////////////////////
// selectors

/*
 * Session Methods: These are to do with the Session Profile. i.e. starting a session (creating a new profile) and ending a session (attempting to transmit
 * the profile info).
 */

// This method initiates logging, this must be called after the configuration methods but before any other logging or any other session methods.
-(void)onStartSessionWithApplicationId :(NSString*)applicationId;
-(void)onStartSessionWithApplicationId :(NSString*)applicationId
                        eventParameters:(NSDictionary*)eventParameters
                            eventDetail:(NSString*)eventDetail
                             eventValue:(NSString*)eventValue;

// This method indicates that the session has ended and will initiate the transmitting of data to the specified url.
-(void)onEndSession;
-(void)onEndSessionWithEventParameters:(NSDictionary*)eventParameters
                           eventDetail:(NSString*)eventDetail
                            eventValue:(NSString*)eventValue;

// This method indicates that the session has entered an idle state, for example the application has lost focus.
-(void)onIdle;
-(void)onIdleWithEventParameters:(NSDictionary*)eventParameters
                     eventDetail:(NSString*)eventDetail
                      eventValue:(NSString*)eventValue;
// special method which can be used to send as the app is being sent to the background
-(void)onIdleWithEventParameters:(NSDictionary*)eventParameters
                     eventDetail:(NSString*)eventDetail
                      eventValue:(NSString*)eventValue
                  fromBackground:(BOOL)fromBackground;

// This method indicates that the session has resumed after an idle state, for example the application has gained focus after losing focus.
-(void)onResume;
-(void)onResumeWithEventParameters:(NSDictionary*)eventParameters
                       eventDetail:(NSString*)eventDetail
                        eventValue:(NSString*)eventValue;

/*
 * Logging Methods: These are to do with logging events such as video start and stop
 */

// This method logs an onDemandVideoStart event.
-(void)onDemandVideoStart;
-(void)onDemandVideoStartWithEventParameters:(NSDictionary*)eventParameters
                                 eventDetail:(NSString*)eventDetail
                                  eventValue:(NSString*)eventValue;

// This method logs an onDemandVideoStop event.
-(void)onDemandVideoStop;
-(void)onDemandVideoStopWithEventParameters:(NSDictionary*)eventParameters
                                eventDetail:(NSString*)eventDetail
                                 eventValue:(NSString*)eventValue;

// This method logs an onLiveVideoStart event.
-(void)onLiveVideoStart;
-(void)onLiveVideoStartWithEventParameters:(NSDictionary*)eventParameters
                               eventDetail:(NSString*)eventDetail
                                eventValue:(NSString*)eventValue;

// This method logs an onLiveVideoStop event.
-(void)onLiveVideoStop;
-(void)onLiveVideoStopWithEventParameters:(NSDictionary*)eventParameters
                              eventDetail:(NSString*)eventDetail
                               eventValue:(NSString*)eventValue;

// This method increments the page view count.
-(void)onPageView;
-(void)onPageViewWithEventParameters:(NSDictionary*)eventParameters
                         eventDetail:(NSString*)eventDetail
                          eventValue:(NSString*)eventValue;

// This method logs a search event.
-(void)onSearch;
-(void)onSearchWithEventParameters:(NSDictionary*)eventParameters
                       eventDetail:(NSString*)eventDetail
                        eventValue:(NSString*)eventValue;

// method which creates an event of any kind - including custom named events
-(void)onEvent:(NSString *)eventName
   eventDetail:(NSString *)eventDetail
    eventValue:(NSString*)eventValue
eventParameters:(NSDictionary*)eventParameters;

// This method logs an error event.
-(void)onError;
-(void)onErrorWithEventParameters:(NSDictionary*)eventParameters
                          message:(NSString*)message
                       eventClass:(NSString*)eventClass
                     eventErrorId:(NSString*)eventErrorId;
-(void)onErrorWithEventParameters:(NSDictionary*)eventParameters
                          message:(NSString*)message
                       eventClass:(NSString*)eventClass 
                  eventErrorLevel:(NSNumber*)eventErrorLevel
                     eventErrorId:(NSString*)eventErrorId;


// obtain a reference to the singleton
+ (BGOAnalyticsManager *)sharedManager;

// Kicks of a transmission run - setting the appropriate mode as it does so
// this should be a private method
-(void)commenceTransmissionWithMode:(NSString*)mode;

// Set loggingLevel

-(void)setEvents:(NSArray *)arrayOfEvents forLogLevel:(int)loggingLevel;
-(BOOL)shouldLogEvent:(NSString*)eventName;

// Remove compulsory session/idle events
-(void)initialize:(NSString*)applicationId
  eventParameters:(NSDictionary*)eventParameters
      eventDetail:(NSString*)eventDetail
       eventValue:(NSString*)eventValue;

-(void)uninitialize:(NSString*)applicationId
    eventParameters:(NSDictionary*)eventParameters
        eventDetail:(NSString*)eventDetail
         eventValue:(NSString*)eventValue;

@end
